<?php
/**
 * description...
 * 
 * @author Administrator
 * @category  
 * @package DnCacheFile
 */
class DnCacheFile extends DnCache
{
	public $config=array(
		'file'=>'DnCacheFile.data.php',
		'mode'=>'nolimit',
		'prefix'=>'',
		'encode'=>'php', //json //serliazi
	);
	public function _get($key) {
		$key=$this->config['prefix'].$key;
		$datas=$this->include_file($this->config['file']);
		return isset($datas[$key])?$datas[$key]:false;
	}
	public function _set($key, $value, $expire = 0) {
		$key=$this->config['prefix'].$key;
		$datas=$this->get_data();
		if(isset($datas[$key]) && $datas[$key]==$value){
			return true;
		}
		$datas[$key]=$value;
		$this->save_data($datas);
		return true;
		
	}
	public function _delete($key) {
		$key=$this->config['prefix'].$key;
		if(!isset($datas[$key])){
			return false;
		}
		unset($datas[$key]);
		$datas[$key]=$value;
		$this->save_data($datas);
		return true;
	}
	protected function get_data()
	{
		switch($this->config['encode']){
			case 'php':
				return @include($this->config['file']);
				break;
			case 'json':
				return json_decode(@file_get_contents($this->config['file']));
				break;
			case 'serialize':
				return unserialize(@file_get_contents($this->config['file']));
				break;
		}
	}
	protected function save_data($data)
	{
		switch($this->config['encode']){
			case 'php':
				$str='<'.'?php // Auto Gen By '. __CLASS__ ."@".date(DATE_ATOM)."\n";
				$str.="return ".var_export($data,true).';?'.'>';
				file_put_contents($this->config['file'],$str);
				break;
			case 'json':
				file_put_contents($this->config['file'],json_encode($data));
				break;
			case 'serialize':
				file_put_contents($this->config['file'],serialize($data));
				break;
		}
	}
}